package com.c611.smartclassroom.service.Impl;

import com.c611.smartclassroom.mapper.*;
import com.c611.smartclassroom.mapper.Ext.*;
import com.c611.smartclassroom.model.ClassRoom;
import com.c611.smartclassroom.model.ClassRoomExample;
import com.c611.smartclassroom.model.Schedule;
import com.c611.smartclassroom.model.WorkOrder;
import com.c611.smartclassroom.service.ClassRoomService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ClassRoomServiceImpl implements ClassRoomService {


    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    ClassRoomMapper classRoomMapper;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    BuildMapper buildMapper;

    @Autowired
    WorkOrderMapperExt workOrderMapperExt;

    @Autowired
    UserMapperExt userMapperExt;

    @Autowired
    ScheduleMapperExt scheduleMapperExt;

    @Autowired
    GateWayMapperExt gateWayMapperExt;

    @Autowired
    DeviceMapperExt deviceMapperExt;

    @Cacheable(value = "class",key = "'queryAllClassRoomCountByBuildID::' + #buildID")
    public long queryAllClassRoomCountByBuildID(String buildID){
        ClassRoomExample classRoomExample = new ClassRoomExample();
        classRoomExample.createCriteria().andDelflagEqualTo(0).andTbuildidEqualTo(buildID);
        return classRoomMapper.countByExample(classRoomExample);
    }
    /**
     * 根据教学楼ID，查询所有教学楼下的所有教室
     * @param buildID
     * @return
     */
    @Cacheable(value = "class",key = "'queryAllClassRoom::' + #buildID + '::' + #page + '::' + #pageSize",unless = "#result == null ")
    @Override
    public List<ClassRoom> queryAllClassRoom(String buildID,Integer page,Integer pageSize) {

        //分页查询
        RowBounds rowBounds = new RowBounds((page - 1) * pageSize,pageSize);
        ClassRoomExample classRoomExample = new ClassRoomExample();
        classRoomExample.createCriteria().andTbuildidEqualTo(buildID);

        //根据教学楼ID，查询对应的教学楼名字
        String buildName = buildMapper.selectByPrimaryKey(buildID).getTbuildname();

        List<ClassRoom> classRoomList = classRoomMapper.selectByExampleWithRowbounds(classRoomExample,rowBounds);

        //将教室信息总的教学楼ID替换成对应的教学楼名称
        for (int i = 0; i < classRoomList.size(); i++) {
            classRoomList.get(i).setTbuildid(buildName);
        }
        return classRoomList;
    }

    /**
     * 根据教室ID，修改教室信息
     * @param
     * @return
     */
    @CacheEvict(value = {"class","build::class","school::build::class"},allEntries = true)
    @Override
    public int save(ClassRoom classRoom) {
        return classRoomMapper.updateByPrimaryKeySelective(classRoom);
    }

    /**
     * 添加教室
     * @param classRoom
     * @return
     */
    @CacheEvict(value = {"class","build::class","school::build::class"},allEntries = true)
    @Override
    public int addClassRoom(ClassRoom classRoom) {
        return classRoomMapper.insertSelective(classRoom);
//        return classRoomMapper.insert(classRoom);
    }

    /**
     * 根据教室ID，删除教室
     * @param classroomID
     * @return
     */
    @CacheEvict(value = {"class","build::class","school::build::class"},allEntries = true)
    @Override
    public int defClassRoom(String classroomID) {
        ClassRoom classRoom = new ClassRoom();
        classRoom.setClassroomid(classroomID);
        classRoom.setDelflag(1);
        return classRoomMapper.updateByPrimaryKeySelective(classRoom);

    }

    //用于修改教室管理的教室信息时，完全删除教室信息，再重新将教室信息插入数据库。
    @CacheEvict(value = {"class","build::class","school::build::class"},allEntries = true)
    @Override
    public int defClassRoom2(String classroomID) {

        return classRoomMapper.deleteByPrimaryKey(classroomID);
    }

    @Cacheable(value = "class",key = "'queryClassRoomById::' + #buildID ",unless = "#result == null ")
    @Override
    public List<ClassRoom> queryClassRoomById(String buildID) {
        ClassRoomExample classRoomExample = new ClassRoomExample();
        classRoomExample.createCriteria()
                .andTbuildidEqualTo(buildID)
                .andDelflagEqualTo(0);
        return classRoomMapper.selectByExample(classRoomExample);
    }

    @Cacheable(value = "class",key = "'queryClassRoomByClassroomId::' + #classroomID ",unless = "#result == null ")
    @Override
    public ClassRoom queryClassRoomByClassroomId(String classroomID) {

        return classRoomMapper.selectByPrimaryKey(classroomID);
    }

//    @Cacheable(value = "class",key = "'queryAllClassRoomCount'")
    @Override
    public Long queryAllClassRoomCount() {
        ClassRoomExample classRoomExample = new ClassRoomExample();
        classRoomExample.createCriteria().andDelflagEqualTo(0);
        return classRoomMapper.countByExample(classRoomExample);
    }

    @Cacheable(value = "class",key = "'queryAllClassRoom::' + #page + '::' + #pageSize",unless = "#result == null ")
    @Override
    public List<ClassRoom> queryAllClassRoom(Integer page,Integer pageSize) {
        //分页查询
        RowBounds rowBounds = new RowBounds((page - 1) * pageSize,pageSize);
        ClassRoomExample classRoomExample = new ClassRoomExample();
        classRoomExample.createCriteria().andDelflagEqualTo(0);

        //根据教学楼ID，查询对应的教学楼名字
//        String buildName = buildMapper.selectByPrimaryKey(buildID).getTbuildname();

        List<ClassRoom> classRoomList = classRoomMapper.selectByExampleWithRowbounds(classRoomExample,rowBounds);

        //将教室信息总的教学楼ID替换成对应的教学楼名称
//        for (int i = 0; i < classRoomList.size(); i++) {
//            classRoomList.get(i).setTbuildid(buildName);
//        }
        return classRoomList;
    }

    /**
     * 进行连锁更改
     * 工单表的教室id,课表中的教室id，网关表中的教室id，设备表中的教室id,user表中授权教室的id
     */
    @CacheEvict(value = {"workOrder","schedule","gateWay","device"},allEntries = true)
    @Override
    public void updateClassRoomIdOther(String newId, String oldId) {
        /**
         * 修改课表
         */
        workOrderMapperExt.updateClassRoomIdOther(newId, oldId);
        scheduleMapperExt.updateClassRoomIdOther(newId, oldId);
        gateWayMapperExt.updateClassRoomIdOther(newId, oldId);
        deviceMapperExt.updateClassRoomIdOther(newId, oldId);
//        userMapperExt.updateClassRoomIdOther(newId, oldId);
    }

    @Override
    public int checkClassRoomId(String classroomid) {
        ClassRoom classRoom = classRoomMapper.selectByPrimaryKey(classroomid);
        if(classRoom == null) return 0;
        return 1;
    }


}
